/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.KamarDAO;
import com.ivanbiz.dao.KosDAO;
import com.ivanbiz.dao.impl.KamarDAOImpl;
import com.ivanbiz.dao.impl.KosDAOImpl;
import com.ivanbiz.model.Kamar;
import com.ivanbiz.model.Kos;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author Shbt Peterpan
 */
public class JurnalKosKamarPanel extends javax.swing.JPanel {

    KosDAO kosDAO;
    List<Kos> listKos;
    List<Kamar> listKamar;

    public JurnalKosKamarPanel() {
        initComponents();
        loadKos();
        dateChooserDariTanggal.setDate(new Date());
        dateChooserSampaiTanggal.setDate(new Date());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        dateChooserDariTanggal = new com.toedter.calendar.JDateChooser();
        jLabel2 = new javax.swing.JLabel();
        dateChooserSampaiTanggal = new com.toedter.calendar.JDateChooser();
        jLabel4 = new javax.swing.JLabel();
        comboKos = new javax.swing.JComboBox();
        comboKamar = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        buttonLihat = new javax.swing.JButton();

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel3.setText("Laporan Jurnal");

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel1.setText("Dari Tanggal :");

        jLabel2.setText("Sampai Tanggal :");

        jLabel4.setText("Kos :");

        comboKos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboKosActionPerformed(evt);
            }
        });

        jLabel5.setText("Kamar :");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(comboKos, 0, 354, Short.MAX_VALUE)
                    .addComponent(jLabel5)
                    .addComponent(comboKamar, 0, 354, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(dateChooserDariTanggal, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jLabel2)
                    .addComponent(dateChooserSampaiTanggal, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comboKos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comboKamar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dateChooserDariTanggal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dateChooserSampaiTanggal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        buttonLihat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/lihat_laporan.jpg"))); // NOI18N
        buttonLihat.setText("Lihat Laporan");
        buttonLihat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonLihatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addComponent(buttonLihat, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonLihat)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonLihatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonLihatActionPerformed
        if (comboKamar.getSelectedItem()==null) {
            JOptionPane.showMessageDialog(this, "Kos / Kamar belum dipilih");
        } else {
            JDialog jDialog = new JDialog();
            jDialog.setModal(true);
            jDialog.setSize(800, 600);
            Kos kos = listKos.get(comboKos.getSelectedIndex());
            jDialog.add(new JurnalReportPanel(jDialog, kos.getKodeKos() + comboKamar.getSelectedItem().toString(), dateChooserDariTanggal.getDate(), dateChooserSampaiTanggal.getDate()));
            jDialog.setLocationRelativeTo(null);
            jDialog.setVisible(true);
        }
    }//GEN-LAST:event_buttonLihatActionPerformed

    private void comboKosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboKosActionPerformed
        try {
            KamarDAO kamarDAO = new KamarDAOImpl();
            listKamar = kamarDAO.getDataByKosAndPenghuni(listKos.get(comboKos.getSelectedIndex()).getId());
            updateComboKamar();
        } catch (Exception ex) {
            Logger.getLogger(JurnalKosKamarPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_comboKosActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonLihat;
    private javax.swing.JComboBox comboKamar;
    private javax.swing.JComboBox comboKos;
    private com.toedter.calendar.JDateChooser dateChooserDariTanggal;
    private com.toedter.calendar.JDateChooser dateChooserSampaiTanggal;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    private void loadKos() {
        try {
            kosDAO = new KosDAOImpl();
            listKos = kosDAO.getAll(Kos.class);
            Kos kos = new Kos();
            kos.setNama("");
            listKos.add(0, kos);
            updateComboKos();
        } catch (Exception ex) {
            Logger.getLogger(JurnalKosKamarPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void updateComboKos() {
        Object data[] = new Object[listKos.size()];
        int x = 0;
        for (Kos kos : listKos) {
            data[x] = kos.getNama();
            x++;
        }
        comboKos.setModel(new DefaultComboBoxModel(data));
    }

    private void updateComboKamar() {
        Object data[] = new Object[listKamar.size()];
        int x = 0;
        for (Kamar kamar : listKamar) {
            data[x] = kamar.getKodeKamar();
            x++;
        }
        comboKamar.setModel(new DefaultComboBoxModel(data));
    }
}
